package main.Web;

import com.alibaba.fastjson.JSON;
import main.Service.UserService;
import main.ServiceImpli.UserServiceImpl;
import main.pojo.User;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

/**
 * 作者: ALIM-MASTIK
 * 创建实现: 2023/7/26 22:40
 * 项目名称：chat
 * 软件: IntelliJ IDEA
 */
@WebServlet("/SetFriend")
public class SetFriend extends HttpServlet {
    UserService userService=new UserServiceImpl();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setHeader("Access-Control-Allow-Origin", "*");
        Map<String,Object>map = new HashMap<>();
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
        String username=req.getParameter("username");
//        System.out.println("username:"+username);
        User user=userService.getUserByUsername(username);
//        System.out.println(user.toString());
        req.getSession().setAttribute("Friend",user);
        map.put("flag",true);
        String json = JSON.toJSONString(map);
        PrintWriter pw = resp.getWriter();
        pw.print(json);
    }
}
